/**
 * Created by zhijie on 2016/12/15.
 */
/**
 * Created by hezhijie on 2016/11/22.
 */
var payPageModule = angular.module("payPageModule", []);
payPageModule.controller("payPageCtrl", ['$scope',
    function ($scope) {
        $scope.init = function () {
            getMerchant();
        }

        function getMerchant() {
            $post(basePath + "/rest/merchant/_get", {
                id: $scope.$stateParams.merchantId
            }, function (result) {
                $scope.merchant = result.resultValue;
                $scope.$apply();
            })
        }

        $scope.toPayApp = function () {
            try {
                if ($scope.totalAmount) {
                    $scope.$apply(function () {
                        $scope.totalAmount = Number($scope.totalAmount).toFixed(2);
                    });
                    var totalAmount = $scope.totalAmount * 100;
                    if (totalAmount < 0) {
                        showMessage("请输入正确金额");
                    }
                    if ($scope.$stateParams.merchantId) {
                        $post(basePath + "/rest/order/_paySM", {
                            merchantId: $scope.$stateParams.merchantId,
                            totalAmount: totalAmount.toFixed(0)
                        }, function (result) {
                            location.href = result.resultValue;
                        })
                    } else {
                        showMessage("商户已失效");
                    }
                }
            } catch (e) {
                showMessage("请输入正确金额");
            }
        }

        $scope.formatPayAmount = function () {
            try {
                if ($scope.totalAmount) {
                    $("#toPayAppBtn").attr("disabled", false)
                    return Number($scope.totalAmount).toFixed(2);
                } else {
                    $("#toPayAppBtn").attr("disabled", true)
                    return "0.00";
                }
            } catch (e) {
                return "0.00";
            }
        }

        $scope.amountChange = function () {
            if ($scope.totalAmount&&angular.isNumber($scope.totalAmount)) {
                if (!/^\d{0,6}\.\d{0,2}$|^\d{0,6}$/.test($scope.totalAmount)) {
                    $scope.totalAmount = Number(Number($scope.totalAmount - 0.005).toFixed(2));
                }
                if($scope.totalAmount<0){
                    $scope.totalAmount=$scope.totalAmount*-1
                }
            }else{
                $scope.totalAmount=0
            }
        }

        $scope.init();
    }
]);
